package com.commander.spring.cloud.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * description:
 * <p>
 * Date: 2020-06-04
 * <p>
 * Time: 18:59
 *
 * @author zhangxin
 */
@Configuration
public class DruidConfig {
	
	/**
	 * 将配置文件中的连接池配置添加到数据源
	 */
	@ConfigurationProperties(prefix = "druid.datasource")
	@Bean
	public DataSource dataSource(){
		return new DruidDataSource();
	}
	
	/**
	 * 配置一个管理后台的Servlet
	 * 配置Druid的监控
	 */
	@Bean
	public ServletRegistrationBean statViewServlet(){
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
		servletRegistrationBean.setServlet(new StatViewServlet());
		// 配置管理后台路径
		servletRegistrationBean.setUrlMappings(Arrays.asList("/druid/*"));
		Map<String,String> initParams = new HashMap<>();
		initParams.put("loginUsername","admin");
		initParams.put("loginPassword","123456");
		// 配置允许访问的路径，默认允许所有访问
		initParams.put("allow","");
		// 拒绝那些路径访问
		initParams.put("deny","192.168.1.150");
		servletRegistrationBean.setInitParameters(initParams);
		return servletRegistrationBean;
	}
	
	/**
	 *  配置一个web监控的filter
	 */
	@Bean
	public FilterRegistrationBean webStatFilter(){
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		Map<String,String> initParams = new HashMap<>();
		//忽略哪些请求，不过滤的请求
		initParams.put("exclusions","*.js,*.css,/druid/*");
		filterRegistrationBean.setInitParameters(initParams);
		// 过滤所有请求
		filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
		return filterRegistrationBean;
	}
}
